/*
 * Copyright (c) 2006-2021, Halo team
 *
 * SPDX-License-Identifier: GPLv3
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-09-11     Eywen        the first version
 */

#ifndef APPLICATIONS_ENDSTOP_H_
#define APPLICATIONS_ENDSTOP_H_

#include "common.h"
#include "motion_step.h"

typedef enum endstop_st{
    endstop_release,
    endstop_push,
}endstop_status_t;

typedef enum endstop_error{
    endstop_ok = 0,             /* 无错误 */
    endstop_err_uninit,         /* 未初始化 */
    endstop_err_short,          /* 短路 */
    endstop_err_break,          /* 断路 */
    endstop_err_unkown,         /* 未知错误 */
}endstop_err_t;

typedef struct _endstop endstop_t;

typedef struct _endstop_ops
{
    bool (*init)(endstop_t *device, int pin, bool release_level,
                 motion_step_t *step);
    endstop_status_t     (*status)(endstop_t *device);
    endstop_err_t        (*error)(endstop_t *device);
}endstop_ops_t;

typedef struct _endstop{
    int pin;                            /* 引脚号 */
    bool release_level;                 /* 正常释放时电源 */
    endstop_ops_t *ops;                 /* 接口函数 */
    endstop_err_t err_status;           /* 错误状态 */

    motion_step_t *motion_step_relevancy;  /* 关联的步进电机,在触发时可以重置步进电机
                                            位置信息 */
}endstop_t;


bool bind_endstop_motion_step(endstop_t *endstop, motion_step_t *step);

#endif /* APPLICATIONS_ENDSTOP_H_ */
